package com.zkgengkun.console.controller;

import com.google.common.collect.Maps;
import com.zkgengkun.console.service.BlogService;
import com.zkgengkun.console.service.UploadService;
import com.zkgengkun.constant.SysConstants;
import com.zkgengkun.handler.MyJsonException;
import com.zkgengkun.support.component.KdApiOrderDistinguish;
import com.zkgengkun.domain.Blog;
import com.zkgengkun.handler.MyException;
import com.zkgengkun.support.component.AmqpSend;
import com.zkgengkun.support.json.Jacksons;
import com.zkgengkun.support.redis.RedisUtils;
import com.zkgengkun.support.util.JedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gengshikun
 * @date 2016/12/6
 */
@Controller
public class WebIndexController {

    @Autowired
    private AmqpSend amqpSend;

    @Autowired
    private BlogService blogService;

    @Autowired
    private KdApiOrderDistinguish kdApiOrderDistinguish;

    @Autowired
    private UploadService uploadService;

    @RequestMapping(value = {"", "/index"}, method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "test/url")
    @ResponseBody
    public String testUrl(String url){
        this.uploadService.uploadJpgByUrl(url);
        return "success";
    }

    @RequestMapping(value = "test/s_randmember")
    @ResponseBody
    public String testSrandmember(Integer count){
        List<String> set = JedisUtil.getInstance().SETS.srandmember("test-s_add", count == null ? 1 : count);
        JedisUtil.getInstance().SETS.srem("test-s_add", set.toArray(new String[]{}));
        return Jacksons.json().fromObjectToJson(set);
    }

    @RequestMapping(value = "test/s_add")
    @ResponseBody
    public String testSadd(String value){
        for(int i = 0; i < 100; i++){
            JedisUtil.getInstance().SETS.sadd("test-s_add", i+"");
        }
        return "success";
    }

    @RequestMapping(value = "test/amqp", method = RequestMethod.GET)
    public String testKd() {
        amqpSend.sendMsg("Hello Springboot!");
        Map<String, Object> map = Maps.newHashMap();
        map.put("hello", "Hello Map!");
        amqpSend.sendMap(SysConstants.AMQP_ROUTINGKEY_MAP, map);
        return "index";
    }

    @RequestMapping(value = "test/kd", method = RequestMethod.GET)
    public String testAmqp() {
        try {
            String result = kdApiOrderDistinguish.getOrderTracesByJson("308552291983");

//            Map map = Jacksons.json().fromJsonToMap(result);
//            List list = (List) map.get("Shippers");
//            Map ship = (Map) list.get(0);

            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }


    @RequestMapping(value = "test/exception_json", method = RequestMethod.GET)
    public String json() throws Exception {
        throw new NullPointerException();
    }

    @RequestMapping(value = "test/json", method = RequestMethod.GET)
    public ModelAndView testJson() throws Exception {
        ModelAndView model = new ModelAndView();
        // 转Json
        MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
        model.setView(jsonView);
        model.addObject("status", Boolean.FALSE);
        model.addObject("msg", "失败");
        return model;
    }

    @RequestMapping(value = "test/redis", method = RequestMethod.GET)
    @ResponseBody
    public String testRedis(){

        JedisUtil.getInstance().STRINGS.setEx("boot-test", 100, "redis-test");

        String test = JedisUtil.getInstance().STRINGS.get("boot-test");
        System.out.println(test);

        return "success";
    }

    @RequestMapping(value = "test/redis_service")
    @ResponseBody
    public Map testRedisService(Model model){
        Map<String, Object> result = Maps.newHashMap();
        result.put("blog1", this.blogService.getOne("402881a65aa6cfa1015aa6d3cb350000").getBlogId());
        return result;
    }

    @RequestMapping(value = "test/redis_test")
    @ResponseBody
    public String testRedisTest(){
        Blog blog = this.blogService.getOneByTitle("testRedis");
        return blog.getTitle();
    }

    @RequestMapping(value = "test")
    public String test(){
        return "test";
    }



}
